﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.教师操作BLL;
using Model;



namespace NCRE学生考试端V1._0
{
    public partial class frmLogInfo : Form
    {
        public frmLogInfo()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 11:55:26
        private static frmLogInfo FrmLogInfo = null;
        public static frmLogInfo GetInstance()
        {
            if (FrmLogInfo==null|| FrmLogInfo.IsDisposed)
            {
                FrmLogInfo = new frmLogInfo();
            }
            return FrmLogInfo;
        }
        #endregion

        IPEntity ipEntity = new IPEntity();
        LogBll logbll = new LogBll();
        DataTable table = new DataTable();

        private void cmbPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            label2.Visible = false;
            Boolean flag = false;
            bindDataToGridView(flag);
        }

        private void frmLogInfo_Load(object sender, EventArgs e)
        {
            #region 设置开始时间提前两小时，截至时间晚两小时
            dateTimePicker1.Value = DateTime.Now.AddHours(-2);
            dateTimePicker2.Value = DateTime.Now.AddHours(2);
            #endregion

            //查询IP表中的所有IP信息
            SelectIpBLL selectIpBLL = new SelectIpBLL();
            DataTable dt;
            dt = selectIpBLL.SelectAllIp();

            //IP下拉框赋值前清空
            cmbPlace.Items.Clear();

            //IP下拉框赋值     
            cmbPlace.SelectedIndexChanged -= new EventHandler(cmbPlace_SelectedIndexChanged);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbPlace.Items.Add(dt.Rows[i][1].ToString());
            }
            cmbPlace.Items.Add("全部");
            cmbPlace.Text = "全部";
            cmbPlace.SelectedIndexChanged += new EventHandler(cmbPlace_SelectedIndexChanged);

            //查询日志信息
            IPStaticEntity.IpName = cmbPlace.Text;
            string startTime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endTime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            DataTable table = logbll.selectNoLogInfo(startTime, endTime);
            if (table == null || table.Rows.Count <= 0)
            {
                label2.Visible = true;
                return;
            }
            else
            {
                cRows = table.Rows.Count;
                cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                PageCurrent = 1;
                toolStripLabel3.Text = cRows.ToString();
                LoadData();
                dataGridView1.AllowUserToAddRows = false;
            }
            
        }
                    

        public void bindDataToGridView(Boolean flag)
        {            
            IPStaticEntity.IpName = cmbPlace.Text;
            if (flag == true)
            {               
                string startTime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
                string endTime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
                #region 当下拉菜单中不选择“全部”时，执行以下代码
                if (cmbPlace.Text != "全部")
                {                   
                    String ip = "";
                    if (cmbPlace.Text != "")
                    {
                        SelectIpBLL selectIpBLL = new SelectIpBLL();
                        DataTable dt;
                        
                        dt = selectIpBLL.SelectOneIp(ipEntity);
                        ip = dt.Rows[0][0].ToString();                       
                    }
                    else
                    {
                        MessageBox.Show("请输入ip");
                        return;
                    }
                    //DataTable dt_studentBind = logbll.selectLogInfo(startTime, endTime, ip);
                    table = logbll.selectLogInfo(startTime, endTime, ip);
                    if (table == null || table.Rows.Count <= 0)
                    {
                        label2.Visible = true;
                        return;
                    }
                    cRows = table.Rows.Count;
                    cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                    PageCurrent = 1;
                    toolStripLabel3.Text = cRows.ToString();
                    LoadData();
                    dataGridView1.AllowUserToAddRows = false;
                }
                
                #endregion

                #region 当下拉菜单中选择“全部”时，执行以下代码
                else
                {
                    table = logbll.selectNoLogInfo(startTime, endTime);
                    if (table == null || table.Rows.Count <= 0)
                    {
                        label2.Visible = true;
                        return;
                    }
                    cRows = table.Rows.Count;
                    cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                    PageCurrent = 1;
                    toolStripLabel3.Text = cRows.ToString();
                    LoadData();
                    dataGridView1.AllowUserToAddRows = false;
                }
            }
                #endregion
                
            else
            {
                return;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null; 
            bindDataToGridView(true);
        }

        #region 分页功能
        //定义分页需要的变量
        int cRows = 0;
        int RowCurrent = 0;
        int cPages = 0;
        int PageCurrent = 0;
        const int PageSize = 20;
        int record = 0;


        private void LoadData()
        {
            if (PageCurrent == 1)
            {
                ts_PagePrevious.Enabled = false;
            }
            else
            {
                ts_PagePrevious.Enabled = true;
            }
            if (cPages == PageCurrent)
            {
                ts_PageNext.Enabled = false;
            }
            else
            {
                ts_PageNext.Enabled = true;
            }

            ts_PagePostion.Text = PageCurrent.ToString();
            ts_Pages.Text = cPages.ToString();
            int startRow = (PageCurrent - 1) * PageSize;
            int endRow = PageCurrent == cPages ? cRows : PageCurrent * PageSize;
            DataTable dtTemp = table.Clone();
            if (table.Rows.Count != 0)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    dtTemp.ImportRow(table.Rows[i]);
                }
                bindingSource1.DataSource = dtTemp;
                dataGridView1.DataSource = bindingSource1;

            }

        }

        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                ts_PageNext.Enabled = true;
                if (PageCurrent == 1)
                {
                    ts_PagePrevious.Enabled = false;
                    MessageBox.Show("已经是第一页了");
                    return;
                }
                PageCurrent--;
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                ts_PagePrevious.Enabled = true;
                if (PageCurrent == PageSize)
                {
                    ts_PageNext.Enabled = false;
                    MessageBox.Show("已经是最后一页了");
                    return;
                }
                PageCurrent++;
                LoadData();
            }
        }

        private void bindingNavigatorMoveFirstPage_Click(object sender, EventArgs e)
        {
            if (PageCurrent == 1)
            {
                MessageBox.Show("已经是第一页了");
                return;
            }
            else
            {
                PageCurrent = 1;
                bindingNavigatorMoveFirstPage.Enabled = false;
                bindingNavigatorMoveLastPage.Enabled = true;
                ts_PagePrevious.Enabled = false;
                ts_PageNext.Enabled = true;
                LoadData();
            }
        }

        private void bindingNavigatorMoveLastPage_Click(object sender, EventArgs e)
        {
            if (PageCurrent == PageSize)
            {
                MessageBox.Show("已经是最后一页了");
                return;
            }
            else
            {
                PageCurrent = cPages;
                bindingNavigatorMoveFirstPage.Enabled = true;
                bindingNavigatorMoveLastPage.Enabled = false;
                ts_PagePrevious.Enabled = true;
                ts_PageNext.Enabled = false;
                LoadData();
            }
        }

        private void ts_PagePostion_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= cPages)
                {
                    PageCurrent = Convert.ToInt32(ts_PagePostion.Text);
                    LoadData();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (System.Exception ex)
            {
                PageCurrent = 1;
                LoadData();
            }
        }
        #endregion
    }
}
